﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Dao;
using Core.Entity;
using System.Data;

namespace Core.Business
{
    public class ServiceBus
    {
        private ServiceDao dao;
        public ServiceBus()
        {
            dao = new ServiceDao();
        }

        public bool insert(Service service) {
            return dao.insert(service);
        }

        public bool update(Service service) {
            return dao.update(service);
        }

        public bool delete(int id) {
            return dao.delete(id);
        }

        public bool delete(String ids) {
            return dao.delete(ids);
        }

        public Service get(int id) {
            DataTable dt = dao.get(id);
            Service service = new Service();

            if (dt.Rows.Count > 0)
            {
                service.Id = Convert.ToInt32(dt.Rows[0]["SVC_ID"]);
                service.ServiceTypeId = Convert.ToInt32(dt.Rows[0]["SVT_ID"]);
                service.Name = dt.Rows[0]["SVC_NAME"].ToString();
                service.Price = Convert.ToDouble(dt.Rows[0]["SVC_PRICE"]);
                service.Desc = dt.Rows[0]["SVC_DESC"].ToString();
            }
            return service;
        }

        public List<Service> getAll() {
            DataTable dt = dao.getAll();
            List<Service> list = new List<Service>();

            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    Service service = new Service();
                    service.Id = Convert.ToInt32(dr["SVC_ID"]);
                    service.ServiceTypeId = Convert.ToInt32(dr["SVT_ID"]);
                    service.Name = dr["SVC_NAME"].ToString();
                    service.Price = Convert.ToDouble(dr["SVC_PRICE"]);
                    service.Desc = dr["SVC_DESC"].ToString();

                    list.Add(service);
                }

            }
            return list;
        }

        public Service getByServiceType(int serviceTypeId) 
        {
            DataTable dt = dao.get(serviceTypeId);
            Service service = new Service();

            if (dt.Rows.Count > 0)
            {
                service.Id = Convert.ToInt32(dt.Rows[0]["SVC_ID"]);
                service.ServiceTypeId = Convert.ToInt32(dt.Rows[0]["SVT_ID"]);
                service.Name = dt.Rows[0]["SVC_NAME"].ToString();
                service.Price = Convert.ToDouble(dt.Rows[0]["SVC_PRICE"]);
                service.Desc = dt.Rows[0]["SVC_DESC"].ToString();
            }
            return service;
        }
    }
}
